#include "InstructionsService.h"

InstructionsService::InstructionsService()
{
    //构造函数里需要关联信号
    m_socketClient = new SocketClient();

    connect(m_socketClient, SIGNAL(dataZijianPacketReady(SocketPacket)),
            this, SLOT(dataZijianPacketReady(SocketPacket)));

    connect(m_socketClient, SIGNAL(dataYingguangPacketReady(SocketPacket)),
            this, SLOT(dataYingguangPacketReady(SocketPacket)));

    connect(m_socketClient, SIGNAL(dataShiyanYingguangPacketReady(SocketPacket)),
            this, SLOT(dataShiyanYingguangdataPacket(SocketPacket)));

    connect(m_socketClient, SIGNAL(dataKuozengPacketReady(SocketPacket)),
            this, SLOT(dataKuozengPacketReady(SocketPacket)));

    connect(m_socketClient, SIGNAL(dataYangbenPacketReady(SocketPacket)),
            this, SLOT(dataYangbenPacketReady(SocketPacket)));

    connect(m_socketClient, SIGNAL(dataWenduPacketReady(SocketPacket)),
            this,SLOT(dataWenduPacketReady(SocketPacket)));

    connect(m_socketClient, SIGNAL(dataWeihuPacketReady(SocketPacket)),
            this,SLOT(dataWeihuPacketReady(SocketPacket)));

    connect(m_socketClient, SIGNAL(dataIPPacketReady(SocketPacket)),
            this,SLOT(dataIPPacketReady(SocketPacket)));

}

InstructionsService::~InstructionsService()
{
}


void InstructionsService::sendYingguangData(QString deviceID, QString shiyanID){
    m_socketClient->sendYingguangData(cov.decimalToHex4Bit(deviceID.toInt()),cov.decimalToHex4Bit(shiyanID.toInt()));
}

/*!
 * \brief 用于发送请求温度指令
 * param shiyanID 实验ID
 * param content 数据内容
 *
 * Detail here!!!
 */
void InstructionsService::sendWenduData(QString diviceId, QString shiyanId){
    m_socketClient->sendWenduData(cov.decimalToHex4Bit(diviceId.toInt()),cov.decimalToHex4Bit(shiyanId.toInt()));
}


/*!
 * \brief 槽函数，用于接收温度回传数据
 *
 *
 *
 * Detail here!!!
 */
void InstructionsService::dataWenduPacketReady(SocketPacket packet){
    wenDuData = m_socketClient->analyWenduData(packet);
}

QStringList InstructionsService::getWenduData(){
    return wenDuData;
}


/*!
 * \brief 用于发送样本数据
 * param shiyanID 实验ID
 * param content 数据内容
 *
 * Detail here!!!
 */
void InstructionsService::sendYangbenData(QString shiyanID, QString content){

    m_socketClient->sendYangbenData(cov.decimalToHex4Bit(shiyanID.toInt()),content);
}

QStringList InstructionsService::getYangbenData(){
    return yangBenData;
}

/*!
 * \brief 槽函数，用于接收样本回传数据
 *
 *  将荧光数据存储到成员变量yangBengData中
 *
 * Detail here!!!
 */
void InstructionsService::dataYangbenPacketReady(SocketPacket packet)
{

    yangBenData = m_socketClient->analyYangbenData(packet);
    //    qDebug() << yangBengData;
}




/*!
 * \brief 用于发送扩增数据
 * param shiyanID 实验ID
 * param content 数据内容
 *
 * Detail here!!!
 */
void InstructionsService::sendKuozengData(QString shiyanID, QString content){
    m_socketClient->sendKuozengData(cov.decimalToHex4Bit(shiyanID.toInt()),content);
}


/*!
 * \brief 槽函数，用于接收扩增回传数据
 *
 *  将荧光数据存储到成员变量kuoZengData中
 *
 * Detail here!!!
 */
void InstructionsService::dataKuozengPacketReady(SocketPacket packet)
{

    kuoZengData = m_socketClient->analyKuozengData(packet);
    qDebug() << kuoZengData;
}


/*!
 * \brief 获取荧光数据
 *
 * * \return QList<YingGuang> 若存在应该数据则返回存储着各阶段数据的QList，
 *                            且每个阶段的数据用结构体存储
 *
 * Detail here!!!
 */
QStringList InstructionsService::getKuoZengData()
{
    if(kuoZengData.size() == 0)
    {
        qDebug()<<"未收到扩增回传数据";
    }
    return kuoZengData;
}




/*!
 * \brief 槽函数，用于接收荧光数据
 *
 *将荧光数据存储到成员变量yingguangData中
 *
 * Detail here!!!
 */
void InstructionsService::dataYingguangPacketReady(SocketPacket packet)
{

    yingguangData = m_socketClient->parseYingguangdataPacket(packet);
    //    qDebug() << yingguangData;
    saveYingGuangData();
}

/*!
 * \brief 槽函数，用于实验恢复荧光数据
 *
 *将荧光数据存储到成员变量yingguangData中
 *
 * Detail here!!!
 */
void InstructionsService::dataShiyanYingguangdataPacket(SocketPacket packet)
{

    shiYanYingguangData = m_socketClient->parseShiyanYingguangdataHuifuPacket(packet);
    //    qDebug() << "shiYanYingguangData0000000" << shiYanYingguangData << shiYanYingguangData.size();
    saveShiYanHuiFuData();
    //    qDebug() << "shiYanYingguangData1111111" << shiYanYingguangData;
}

/*!
 * \brief 保存荧光数据
 *
 *将荧光数据转为十进制保存到dataList中
 *
 * Detail here!!!
 */
void InstructionsService::saveYingGuangData()
{
    int len = (yingguangData.size()-1);


    for (int i=1; i<len; i+=2)
    {
        YingGuang p1;
        p1.stage =  cov.hexToDec(yingguangData[i][0].toStdString());
        p1.number = cov.hexToDec(yingguangData[i][1].toStdString());
        p1.step = cov.hexToDec(yingguangData[i][2].toStdString());
        p1.cycle_number = cov.hexToDec(yingguangData[i][3].toStdString());


        //只设置32个荧光孔位信息
        for (int row=0; row<32; row++)
        {
            QString column_data = yingguangData[i+1][row];
            QHash<QString,QString> stage_data;

            QString num1 = column_data.mid(4,4);
            QString num2 = column_data.mid(8,4);
            QString num3 = column_data.mid(12,4);
            QString num4 = column_data.mid(16,4);

            stage_data.insert("4",cov.hexToDec(num1.toStdString()));
            stage_data.insert("3",cov.hexToDec(num2.toStdString()));
            stage_data.insert("2",cov.hexToDec(num3.toStdString()));
            stage_data.insert("1",cov.hexToDec(num4.toStdString()));

            p1.sample_wells.append(stage_data);
        }

        yingGuangDataList.append(p1);
    }

}

/*!
 * \brief 保存实验恢复荧光数据
 *
 *将荧光数据转为十进制保存到shiyanHuiFuList中
 *
 * Detail here!!!
 */
void InstructionsService::saveShiYanHuiFuData()
{
    int len = shiYanYingguangData.size();
    HuiFuData huifu;

    qDebug() << "shiYanYingguangData" << shiYanYingguangData;
    qDebug() << "shiYanYingguangData" << shiYanYingguangData.size();

    // 判断解析数据是否正确
    if(len == 2){
        QStringList parseYingguangList = shiYanYingguangData[1]; // 获取荧光
        QList<QString> judgeYingguangList; // 判断荧光数据列表：只存荧光
        // 写入荧光
        if(parseYingguangList.size() == 32){ // 荧光数据长度正确
            for(int i = 0; i < parseYingguangList.size(); i++){
                QString parseYingguang = parseYingguangList[i];
                if(parseYingguang != ""){ // 荧光值不为空
                    QHash<QString, QString> kongWeiMap2YingGuang; // 孔位映射荧光

                    QString num1 = parseYingguang.mid(4,4); // fam通道
                    QString num2 = parseYingguang.mid(8,4); // vic通道
                    QString num3 = parseYingguang.mid(12,4); // rox通道
                    QString num4 = parseYingguang.mid(16,4); // cy5通道

                    kongWeiMap2YingGuang.insert("4", cov.hexToDec(num1.toStdString())); // 写入fam通道
                    kongWeiMap2YingGuang.insert("3", cov.hexToDec(num2.toStdString())); // 写入civ通道
                    kongWeiMap2YingGuang.insert("2", cov.hexToDec(num3.toStdString())); // 写入rox通道
                    kongWeiMap2YingGuang.insert("1", cov.hexToDec(num4.toStdString())); // 写入cy5通道

                    // 将荧光数据写入判断数组中
                    judgeYingguangList.append(num1);
                    judgeYingguangList.append(num2);
                    judgeYingguangList.append(num3);
                    judgeYingguangList.append(num4);

                    huifu.sample_wells.append(kongWeiMap2YingGuang); // 写入荧光值
                }else { // 荧光值为空，数据有误，直接结束数据恢复
                    huifu.deviceID = "";
                    judgeYingguangList.clear();
                    break;
                }
            }
        }

        // 根据去重后的判断荧光列表，判断荧光数据是否都为0
        judgeYingguangList = judgeYingguangList.toSet().toList();
        if(judgeYingguangList.size() < 2){ // 如果去重后的数组长度小于2（=0 | =1），说明该回传荧光值为0（为空）或数据不对
            huifu.deviceID = ""; // 设备ID回传空，ui层判断失败
        }else { // 荧光数据存在
            QStringList otherInfo = shiYanYingguangData[0]; // 除荧光之外的其他信息
            if(otherInfo.size() == 3){ // 其他信息数据正确
                // 写入恢复结构体
                huifu.deviceID = otherInfo[0]; // 设备id
                huifu.shiyanID = otherInfo[1]; // 实验id
                huifu.cycle_number = otherInfo[2]; // 循环数
            }
        }

        // 写入回传数组中
        shiyanHuiFuList.append(huifu);
    }

}

/*!
 * \brief 获取荧光数据
 *
 * * \return QList<YingGuang> 若存在应该数据则返回存储着各阶段数据的QList，
 *                            且每个阶段的数据用结构体存储
 *
 * Detail here!!!
 */
QList<YingGuang> InstructionsService::getYingGuangData()
{
    if(yingGuangDataList.size() == 0)
    {
        qDebug()<<"未收到荧光数据";
    }
    return yingGuangDataList;
}

/*!
 * \brief 获取恢复实验荧光数据
 *
 * * \return QList<YingGuang> 若存在应该数据则返回存储着各阶段数据的QList，
 *                            且每个阶段的数据用结构体存储
 *
 * Detail here!!!
 */
QList<HuiFuData> InstructionsService::getShiYanHuiFuData()
{
    if(shiyanHuiFuList.size() == 0)
    {
        qDebug()<<"未收到恢复荧光数据";
    }
    return shiyanHuiFuList;
}



/*!
 * \brief 与下位机建立连接
 *
 * \param QString ipAddress  下位机ip地址   int port 下位机端口号
 *
 * \return true 连接成功   flase 连接失败
 *
 * Detail here!!!
 */
bool InstructionsService::establishConnection(QString ipAddress, int port)
{
    bool connected = m_socketClient->isConnected();
    if(!connected)
    {
        QHostAddress ip(ipAddress);
        m_socketClient->connectTcpServer(ip, port);

        qDebug() << "建立连接" << endl;
    }
    return m_socketClient->isConnected();
}

void InstructionsService::sleep(){
    QTime dieTime = QTime::currentTime().addMSecs(sleepTime);
    while( QTime::currentTime() < dieTime )
        QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}

/*!
 * \brief 向下位机发送自检命令
 *
 * \param QString id  设备id 例子：0001
*
 * Detail here!!!
 */
void InstructionsService::sendChecking(QString id)
{
    m_socketClient->sendZijianCmd(cov.decimalToHex4Bit(id.toInt()));
}

/*!
 * \brief 向下位机发送暂停命令
 *
 * \param QString id  设备id 例子：0001
 *
 * Detail here!!!
 */
void InstructionsService::sendStop(QString deviceID, QString shiyanID,QString wendu)
{
    m_socketClient->sendStopCmd(cov.decimalToHex4Bit(deviceID.toInt()),cov.decimalToHex4Bit(shiyanID.toInt()),cov.decimalToHex4Bit(wendu.toInt() * 10));
}


/*!
 * \brief 向下位机发送停止命令
 *
 * \param QString id  设备id 例子：0001
 *
 * Detail here!!!
 */
void InstructionsService::sendClose(QString deviceID, QString shiyanID)
{
    m_socketClient->sendCloseCmd(cov.decimalToHex4Bit(deviceID.toInt()),cov.decimalToHex4Bit(shiyanID.toInt()));
}

/*!
 * \brief 向下位机发送请求故障信息命令
 *
 * \param QString id  设备id 例子：0001
 *
 * Detail here!!!
 */
void InstructionsService::sendGuzhangData(QString deviceID, QString shiyanID)
{
    m_socketClient->sendGuzhangData(cov.decimalToHex4Bit(deviceID.toInt()),cov.decimalToHex4Bit(shiyanID.toInt()));

}


QStringList InstructionsService::getGuZhangData(){
    return guZhangData;
}

/*!
 * \brief 向下位机发送继续命令
 *
 * \param QString id  设备id 例子：0001
 *
 * Detail here!!!
 */
void InstructionsService::sendContinue(QString deviceID, QString shiyanID)
{
    m_socketClient->sendContiueCmd(cov.decimalToHex4Bit(deviceID.toInt()),cov.decimalToHex4Bit(shiyanID.toInt()));
}

/*!
 * \brief 向下位机发送实验恢复命令
 *
 * \param QString id  设备id 例子：0001
 *
 * Detail here!!!
 */
void InstructionsService::sendShiYanHuiFu(QString deviceID, QString shiyanID){
    m_socketClient->sendShiyanHuifuData(
                cov.decimalToHex4Bit(deviceID.toInt()),
                cov.decimalToHex4Bit(shiyanID.toInt()));
}


/*加热维护*/

void InstructionsService::sendWeihuJiaresi(QString zhanKongbi, QString cmd){
    m_socketClient->sendWeihuJiaresi(cov.decimalToHex4Bit(zhanKongbi.toInt()),cov.decimalToHex4Bit(cmd.toInt()));
}


/*待确定*/
void InstructionsService::sendWeihuSensorwendu(QString wendu1, QString wendu2, QString cmd){
    m_socketClient->sendWeihuSersorwendu(cov.decimalToHex4Bit(wendu1.toInt()), cov.decimalToHex4Bit(wendu2.toInt()), cov.decimalToHex4Bit(cmd.toInt()));
}

void InstructionsService::sendWeihuRaoluanfeng(QString zhanKongbi, QString cmd){
    m_socketClient->sendWeihuRaoluanfeng(cov.decimalToHex4Bit(zhanKongbi.toInt()),cov.decimalToHex4Bit(cmd.toInt()));
}

void InstructionsService::sendWeihuRaoluanfengshanKaiguan(QString Kaiguan, QString cmd){
    m_socketClient->sendWeihuRaoluanfengshanKaiguan(cov.decimalToHex4Bit(Kaiguan.toInt()),cov.decimalToHex4Bit(cmd.toInt()));
}

void InstructionsService::sendWeihuPaifengfengshan(QString Kaiguan, QString cmd){
    m_socketClient->sendWeihuPaifengfengshan(cov.decimalToHex4Bit(Kaiguan.toInt()),cov.decimalToHex4Bit(cmd.toInt()));
}

void InstructionsService::sendWeihuDuiliufengshan(QString Zhankongbi, QString cmd){
    m_socketClient->sendWeihuDuiliufengshan(cov.decimalToHex4Bit(Zhankongbi.toInt()),cov.decimalToHex4Bit(cmd.toInt()));
}

void InstructionsService::sendWeihuBiaozhunWendu(QString wendu, QString cmd){
    m_socketClient->sendWeihuBiaozhunWendu(cov.decimalToHex4Bit(wendu.toDouble() * 10),cov.decimalToHex4Bit(cmd.toInt()));
}

void InstructionsService::sendWeihuReshiyuandian(QString tongdao,QString step,QString cmd){
    m_socketClient->sendWeihuReshiyuandian(cov.decimalToHex4Bit(tongdao.toInt()),cov.decimalToHex4Bit(step.toInt()),cov.decimalToHex4Bit(cmd.toInt()));
}

//void InstructionsService::sendWeihuXuanzhuan(QString zhuansu, QString fangxiang, QString cmd){
//    m_socketClient->sendWeihuXuanzhuan(cov.decimalToHex4Bit(zhuansu.toInt()),cov.decimalToHex4Bit(fangxiang.toInt()),cov.decimalToHex4Bit(cmd.toInt()));
//}

void InstructionsService::sendWeihuKongweipiancha(QString haocaitype, QString kongwei, QString yuandianpiancha,
                                                  QString pingjunpiancha, QString cmd){
    m_socketClient->sendWeihuKongweipiancha(cov.decimalToHex4Bit(haocaitype.toInt()), cov.decimalToHex4Bit(kongwei.toInt()), cov.decimalToHex4Bit(yuandianpiancha.toInt()),
                                            cov.decimalToHex4Bit(pingjunpiancha.toInt()), cov.decimalToHex4Bit(cmd.toInt()));
}

/*荧光检测维护*/
void InstructionsService::sendWeihuTongdaoKaiguan(QString tongdao, QString kaiguan, QString cmd){
    m_socketClient->sendWeihuTongdaoKaiguan(cov.decimalToHex4Bit(tongdao.toInt()),cov.decimalToHex4Bit(kaiguan.toInt()),cov.decimalToHex4Bit(cmd.toInt()));
}

void InstructionsService::sendWeihuTongdaozhi(QString tongdao, QString Yingguangzhi, QString AD, QString cmd){
    m_socketClient->sendWeihuTongdaozhi(cov.decimalToHex4Bit(tongdao.toInt()),cov.decimalToHex4Bit(Yingguangzhi.toInt()),cov.decimalToHex4Bit(AD.toInt()),cov.decimalToHex4Bit(cmd.toInt()));
}

void InstructionsService::sendWeihuJifaqiangdu(QString tongdao, QString qiangdu,QString cmd){
    m_socketClient->sendWeihuJifaqiangdu(cov.decimalToHex4Bit(tongdao.toInt()),cov.decimalToHex4Bit(qiangdu.toInt()),cov.decimalToHex4Bit(cmd.toInt()));
}

void InstructionsService::sendWeihuYingguangzengyi(QString tongdao, QString zengyi,QString cmd){
    m_socketClient->sendWeihuYingguangzengyi(cov.decimalToHex4Bit(tongdao.toInt()),cov.decimalToHex4Bit(zengyi.toInt()),cov.decimalToHex4Bit(cmd.toInt()));
}


/*状态检测维护*/
void InstructionsService::sendWeihuMensuo(QString pingbi, QString kaiguan,QString cmd){
    m_socketClient->sendWeihuMensuo(cov.decimalToHex4Bit(pingbi.toInt()),cov.decimalToHex4Bit(kaiguan.toInt()),cov.decimalToHex4Bit(cmd.toInt()));
}

void InstructionsService::sendWeihuBigai(QString pingbi, QString kaiguan,QString cmd){
    m_socketClient->sendWeihuBigai(cov.decimalToHex4Bit(pingbi.toInt()),cov.decimalToHex4Bit(kaiguan.toInt()),cov.decimalToHex4Bit(cmd.toInt()));
}

void InstructionsService::sendWeihuMensuojiaozheng(QString zhankongbi,QString cmd){
    m_socketClient->sendWeihuMensuojiaozheng(cov.decimalToHex4Bit(zhankongbi.toInt()),cov.decimalToHex4Bit(cmd.toInt()));
}

/*IP地址和端口发送*/
void InstructionsService::sendIP(QString ip1, QString ip2, QString ip3, QString ip4, QString port){
    m_socketClient->sendIP(cov.decimalToHex2Bit(ip1.toInt()),cov.decimalToHex2Bit(ip2.toInt()),cov.decimalToHex2Bit(ip3.toInt()),cov.decimalToHex2Bit(ip4.toInt()),cov.decimalToHex4Bit(port.toInt()));
}

void InstructionsService::dataWeihuPacketReady(SocketPacket socketData){
    weiHuData = m_socketClient->analyWeihudataPacket(socketData);

    qDebug() << "weiHuData1" << weiHuData;

    /* 除第一个外，将所有的维护回传数据转为十进制 */
    QStringList weihuDataDec;
    weihuDataDec.append(weiHuData[0]);
    for (int i = 1; i < weiHuData.size(); i++) {
        weihuDataDec.append(cov.hexToDec(weiHuData[i].toStdString()));
    }
    weiHuData = weihuDataDec;

    qDebug() << "weiHuData" << weiHuData;


}

void InstructionsService::dataIPPacketReady(SocketPacket socketData){
    ipData = m_socketClient->analyIPdataPacket(socketData);
}

QString InstructionsService::getIpData(){
    return ipData;
}

QStringList InstructionsService::getWeiHuData(){
    return weiHuData;
}


//read直接给你一个字符串，数据格式为   设备ID；实验ID（这里没有，显示0000）；故障具体字符串
/*!
 * \brief 接收下位机数据
 *
 * \param SocketPacket packet
 *
 *  返回的数据存储在recive里
 * Detail here!!!
 */
void InstructionsService::dataZijianPacketReady(SocketPacket packet)
{
    ziJianData = m_socketClient->analysisData(packet);
    guZhangData = m_socketClient->analysisData(packet);
}



/*!
 * \brief 检查xml文件
 *
 *
 * Detail here!!!
 */
bool InstructionsService::configFileDetect()
{

    path = QCoreApplication::applicationDirPath() + "/config.xml";
    QFile file(path);
    //当xml文件不存在时
    if(! file.exists(path))
    {
        qDebug() << path;
        qDebug()<<"配置文件不存在";
        return false;
    }

    char * filePath;
    QByteArray tempByte = path.toLocal8Bit();
    filePath = tempByte.data();
    try{
        Simplexml * simplexml = new Simplexml(filePath);
    }catch(string s){
        qDebug() << QString::fromStdString(s);
        return false;
    }
    return true;
}

/*!
 * \brief 检查数据库文件和日志文件夹
 *
 *
 * Detail here!!!
 */
bool InstructionsService::dbFileDetect(){
    path = QCoreApplication::applicationDirPath() + "/bokun.db";
    QFile file(path);

    /* 获取并创建日志文件夹 */
    QDir dir;
    QString logPath = QCoreApplication::applicationDirPath();
    string folderPath = logPath.toStdString()+"/logs";
    /* 当日志文件夹不存在 */
    if (! dir.exists(QString::fromStdString(folderPath)))
    {
        //如果logs文件夹不存在，创建新文件夹
        dir.mkpath(QString::fromStdString(folderPath));
    }

    //当数据库文件不存在时
    if (! file.exists(path))
    {
        sqlD = new SqliteDriver();
        sqlD->setDBDir(QCoreApplication::applicationDirPath());
        sqlD->createDB("bokun");
        sqlD->openDB("bokun");
        sqlD->queryExec("CREATE TABLE IF NOT EXISTS device (id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20) NOT NULL,mac VARCHAR(20) NOT NULL,ip VARCHAR(15) NOT NULL, port INTEGER NOT NULL, serial_number VARCHAR(50) NOT NULL,status TINYINT NOT NULL, create_time timestamp NOT NULL DEFAULT(DATETIME('now','localtime')), update_time timestamp NOT NULL DEFAULT(DATETIME('now','localtime')));");
        sqlD->queryExec("CREATE TABLE IF NOT EXISTS experiment (experiment_id INTEGER PRIMARY KEY AUTOINCREMENT,experiment_name VARCHAR(50) NOT NULL,user_name VARCHAR(50),coding VARCHAR(40),device_id INTEGER NOT NULL,type VARCHAR(20) NOT NULL,reaction_volume TINYINT DEFAULT 25,remarks VARCHAR(500),experiment_remarks VARCHAR(500),plate_remarks VARCHAR(500),is_start TINYINT DEFAULT 0,is_end TINYINT DEFAULT 0,create_time timestamp NOT NULL DEFAULT(DATETIME('now','localtime')),update_time timestamp NOT NULL DEFAULT(DATETIME('now','localtime')));");
        sqlD->queryExec("CREATE TABLE IF NOT EXISTS sample ( id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(50) NOT NULL,color_value VARCHAR(20) NOT NULL,remarks VARCHAR(500),create_time timestamp NOT NULL DEFAULT(DATETIME('now','localtime')), update_time timestamp NOT NULL DEFAULT(DATETIME('now','localtime')));");
        sqlD->queryExec("CREATE TABLE IF NOT EXISTS chromosome ( id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(50) NOT NULL,wave_length VARCHAR(20) NOT NULL,creator VARCHAR(50) NOT NULL,type VARCHAR(30) NOT NULL,status TINYINT NOT NULL,create_time timestamp NOT NULL DEFAULT(DATETIME('now','localtime')),update_time timestamp NOT NULL DEFAULT(DATETIME('now','localtime')));");
        sqlD->queryExec("CREATE TABLE IF NOT EXISTS target ( id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(40) NOT NULL,reporter VARCHAR(15) NOT NULL,quencher VARCHAR(15) NOT NULL,explain VARCHAR(150) NOT NULL,type VARCHAR(15) NOT NULL,quantity INTEGER NOT NULL,color VARCHAR(20) NOT NULL,create_time timestamp NOT NULL DEFAULT(DATETIME('now','localtime')),update_time timestamp NOT NULL DEFAULT(DATETIME('now','localtime')));");
        sqlD->queryExec("CREATE TABLE IF NOT EXISTS procedure_stage (id INTEGER PRIMARY KEY AUTOINCREMENT,experiment_id INTEGER NOT NULL,stage_order INTEGER NOT NULL,name VARCHAR(20) NOT NULL, cycle_number INTEGER NOT NULL DEFAULT 0, create_time timestamp NOT NULL DEFAULT(DATETIME('now','localtime')),update_time timestamp NOT NULL DEFAULT(DATETIME('now','localtime')));");
        sqlD->queryExec("CREATE TABLE IF NOT EXISTS procedure_step (id INTEGER PRIMARY KEY AUTOINCREMENT,stage_id INTEGER NOT NULL, step_order INTEGER NOT NULL,temperature FLOAT NOT NULL,rate FLOAT NOT NULL, duration INTEGER NOT NULL,is_fluorescence BOOLEAN NOT NULL,create_time timestamp NOT NULL DEFAULT(DATETIME('now','localtime')),update_time timestamp NOT NULL DEFAULT(DATETIME('now','localtime')));");
        return true;
    }
    return true;

}




